package com.wg.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wg.mapper.ResourceMapper;
import com.wg.pojo.Resource;
import com.wg.vo.SearchResult;
import net.sf.jsqlparser.statement.select.Limit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;


    @Override
    public SearchResult getList(SearchResult searchResult) {
        boolean flag = StringUtils.hasLength(searchResult.getQuery());
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(flag,"resource_title", searchResult.getQuery());
        List<Resource> resources = resourceMapper.selectList(queryWrapper);
        searchResult.setRows(resources);
        return searchResult;
    }

    @Override
    public List<Resource> gethotList() {
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("click_count")
                    .last("limit 0,5");
        return resourceMapper.selectList(queryWrapper);
    }

    @Override
    public List<Resource> getdynamicList(String resourceTitle) {

        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("resource_title", resourceTitle)
                    .last("limit 0,10");
        return resourceMapper.selectList(queryWrapper);
    }
}
